package com.bw.mallshop.mvvm.viewmodel;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.entity.UserEntity;
import com.bw.mallshop.mvvm.model.UserModel;
import com.bw.mallshop.mvvm.view.EdtUserActivity;
import com.bw.mallshop.mvvm.view.LoginActivity;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserViewModel extends BaseViewModel<UserModel> {

    public ObservableField<String>nickName = new ObservableField<>("登录");

    //进入登录界面
    public void login(){
        if (!nickName.get().equals("登录")){
            startPage(EdtUserActivity.class);
            return;
        }
        startPage(LoginActivity.class);
    }

    //进入编辑用户界面
    public void edtUser(){
        startPage(EdtUserActivity.class);
    }

    public void payOrder(){
    }

    public void receivingOrder(){}

    public void completeOrder(){}

    public void userOrder(){}

    public void edtShipe(){}

    @Override
    protected void initData() {
       refresh();
    }

    public void refresh(){
        //获取用户信息数据
        m.loadUser().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof UserEntity){
            //获取到本地数据
            UserEntity userEntity = (UserEntity) entity;
            if (userEntity.data == null){
                nickName.set("登录");
            }else {
                if (TextUtils.isEmpty(userEntity.data.user_nick_name)){
                    nickName.set("--");
                }else
                    nickName.set(userEntity.data.user_nick_name);
            }
        }
    }
}
